#pragma once
#include"rb_tree-6-18.h"

namespace cws_map 
{
	template<class Key, class Value>
	struct KeyOfValue 
	{
		Key operator()(const pair<Key, Value>& kv) 
		{
			return kv.first;
		}
	};


	template<class Key, class Value, class Compare = less<int>>
	class map 
	{
	public:
		typedef pair<Key, Value> PKV;
		typedef Value value_type;
		typedef typename cws_rb_tree::rb_tree<const Key, PKV, KeyOfValue<Key, Value>, Compare>::iterator iterator;
		typedef typename cws_rb_tree::rb_tree<const Key, PKV, KeyOfValue<Key, Value>, Compare>::const_iterator const_iterator;

		iterator begin()
		{
			return _rb.begin();
		}

		iterator end()
		{
			return _rb.end();
		}

		const_iterator begin() const
		{
			return _rb.begin();
		}

		const_iterator end() const
		{
			return _rb.end();
		}

		//
		pair<iterator, bool> insert(const Key key)
		{
			return _rb.insert(key);
		}


		pair<iterator, bool> insert(const PKV& kv) 
		{
			return _rb.insert(kv);
		}
		//
		Value& operator[](const Key& k) 
		{
			auto ret = _rb.insert({k, value_type()});
			return ret.first->second;
		}


	private:
		cws_rb_tree::rb_tree<const Key, PKV, KeyOfValue<Key, Value>, Compare> _rb;
	};

	void test() 
	{
		map<int, int> mp;
		mp.insert({ 3, 2 });
		mp.insert({ 11, 2 });
		mp.insert({ 331, 2 });
		mp.insert({ 121, 2 });
		mp.insert({ 61, 2 });
		mp.insert({ 461, 2 });
		mp.insert({ 161, 2 });
		mp.insert({ 261, 2 });

		int count = 0;
		for (int i = 0; i < 1000; i++) 
		{
			if (i == 971) 
			{
				int x = 1;
			}
			if (mp[i] != 0) 
			{
				cout << i << endl;
			}
		}

	}

}
